<?php

namespace app\admin\logic;

use app\common\model\mysql\ProjectModel;
use app\common\model\mysql\RenovationModel;
use app\common\logic\BaseLogic;
use app\common\service\UploadService;

class RenovationLogic extends BaseLogic
{
    public function index($request)
    {
        $search = $request->param();
        $list = RenovationModel::getPaginate([], '*', 50, ['sort' => 'ASC', 'id' => 'DESC'], ["query" => $search]);
        return view('Renovation/renovation', compact('list', 'search'));
    }

    public function create($request)
    {
        return view('Renovation/renovation_add');
    }

    public function save($request)
    {
        $param = $request->param();
        $inputData = [
            'sort' => $param['sort'],
            'status' => $param['status'],
            'position' => $param['position'],
            'name' => $param['position'] == 'index_banner' ? '首页轮播图' : '首页视频',
        ];
        if ($request->has('index_banner.name', 'file', true)) {
            $file = request()->file('index_banner');
            // 上传到本地服务器
            $inputData['image'] = \think\facade\Filesystem::disk('public')->putFile('index/banner', $file);
        }

        // 视频上传
        if (!empty($param['video'])) {
            $inputData['video'] = $param['video'];
        }
        try {
            RenovationModel::addData($inputData);
        } catch (\Exception $e) {
            return $this->responseError('操作失败');
        }
        return $this->responseSuccess('操作成功');
    }

    public function edit($request, $id)
    {

        $info = RenovationModel::getOne(['id' => $id]);
        return view('Renovation/renovation_edit', compact('info'));
    }

    public function update($request, $id)
    {
        $param = $request->param();
        $inputData = [
            'sort' => $param['sort'],
            'status' => $param['status'],
            'position' => $param['position'],
            'name' => $param['position'] == 'index_banner' ? '首页轮播图' : '首页视频',
        ];
        if ($request->has('index_banner.name', 'file', true)) {
            $file = request()->file('index_banner');
            // 上传到本地服务器
            $inputData['image'] = \think\facade\Filesystem::disk('public')->putFile('index/banner', $file);
        }
        // 视频上传
        if (!empty($param['video'])) {
            $inputData['video'] = $param['video'];
        }
        try {
            RenovationModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }

        return $this->responseSuccess('保存成功');
    }

    public function renew($request, $id)
    {
        $param = $request->param();
        $inputData = [];
        isset($param['sort']) ? $inputData['sort'] = $param['sort'] : false;
        isset($param['status']) ? $inputData['status'] = intval($param['status']) : false;
        try {
            RenovationModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }

    public function delete($request, $id)
    {
        try {
            RenovationModel::deleteData(['id' => $id], true);
        } catch (\Exception $e) {
            return $this->responseError('删除失败');
        }
        return $this->responseSuccess('删除成功');
    }
}